package com.lin.boot.service.impl;

import com.lin.boot.model.ZhihuUser;
import com.lin.boot.repository.ZhihuUserRepository;
import com.lin.boot.service.ZhihuUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@CacheConfig(cacheNames = "zhihuUserService", cacheManager = "cacheManager")
public class ZhihuUserServiceImpl implements ZhihuUserService {

    @Autowired
    private ZhihuUserRepository zhihuUserRepository;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    @Cacheable(cacheManager = "cacheManager", value = "zhihuUserData", key = "#id")
    public ZhihuUser getZhihuUserById(String id) {
        Optional<ZhihuUser> user = zhihuUserRepository.findById(id);
        return user.orElseGet(ZhihuUser::new);
    }
}
